package com.thomas.sharing.flyway.config;



import javax.sql.DataSource;

import org.flywaydb.core.Flyway;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.zaxxer.hikari.HikariDataSource;


@Configuration
//@EnableAutoConfiguration
@EnableConfigurationProperties
public class FlywayConfig {
	
	@Bean
	@Primary
	@ConfigurationProperties(prefix="mydb.datasource")
	public DataSourceProperties dataSourceProperties(){
		return new DataSourceProperties();
	}
	
	@Bean
	public DataSource initDataBase(){
		//return DataSourceBuilder.create().build();
		DataSourceProperties dataSourceProperties = dataSourceProperties();
		DataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
		return dataSource;
	}			
	
	@Bean
	@ConfigurationProperties(prefix="spring.flyway")
	public Flyway getFlyway(){
		return new Flyway();
	}
	
	@Bean("flyway")
	public Flyway initFlyway(){
		Flyway flyway = getFlyway();
		DataSource dataSource = initDataBase();
		flyway.setDataSource(dataSource);
		flyway.migrate();
		return flyway;
	}
	
}
